<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;

use app\services\SspService;
use app\services\AccuenService;

class ResourceSyncController extends AuthController
{
    public $currentHeader = 'header-sync';
    public $currentNav = 'resource-sync-nav';
    public $pageTitle = '';

    public function init() {
        parent::init();
        $this->service = new SspService('vamaker');
    }

    public function actionSyncList() {
        $this->pageTitle = $this->t('resource', 'Adspace Syncronization List');
        return $this->show();
    }

    public function actionListOperateHandle() {
        $AdNetwork = $this->I('id');
        $service = null; 
        $AdNetwork == 1 && $service = $this->service;
        //$AdNetwork == 2 && $service = new AccuenService('Acelink');
        
        if ($service) {
            $service->syncAdspace('bannerAdspace');
            $service->syncAdspace('videoAdspace'); 
            $service->syncAdspace('mobileAdspace');
            $service->syncAdspace('mobileVideoAdspace');
        }
        $this->OK($this->t('resource', 'Adspace Syncronized!'));
    }

    public function actionGetAdNetwork() {
        $data = $this->service->getAll('AdNetwork', ['status' => 1]);
        $matrix = [
            'id'        => 'id',
            'channel'   => ['name', '$v=="vamaker"&&$v=\Yii::t("resource", "ValueMaker");
                                     return $v;'],
            'period' => ['id', 'return \Yii::t("resource", "2 times per week");'],
            'operate' => ['id', 'return "refresh";'],
        ];
        $data = $this->fieldsMap($matrix, $data);
        $this->jsonReturn(['data' => $data]);
    }
}// end
